package jnBase.view 
{
	import jnBase.Data;
	import jnBase.InsSceneTools;
	import jnBase.eventdispatcher.JNEventDispatcher;
	import jnBase.script.MouseOverShowMsg;
	import jnBase.script.ScriptSceneTools;
	import jnBase.user.User;
	
	import laya.display.Sprite;
	import laya.events.Event;
	import laya.filters.GlowFilter;
	import laya.maths.Point;
	import laya.media.SoundManager;
	import laya.ui.Button;
	import laya.ui.Image;
	import laya.utils.Ease;
	import laya.utils.Handler;
	import laya.utils.Tween;
	
	import ui.tools.SceneToolsUI;
	
	public class SceneToolControl extends SceneToolsUI implements InsSceneTools
	{
		/**实验步骤显示对象集合*/		
		private var stepControlList:Array=[];
		private static var _ins:SceneToolControl;
		private var qiCaiData:Array;
		private var dlg_tools:ToolsBox;
		private var _script:ScriptSceneTools;
		public function SceneToolControl()
		{
			super();
			this.mouseThrough=true;
		}
		public static function getInstance():SceneToolControl{
			if(_ins==null){
				_ins=new SceneToolControl();
			}
			return _ins;
		}
		override public function onAwake():void
		{
			super.onAwake();
			_script=this.addComponent(ScriptSceneTools);
			//实验步骤
			ms=new Image("comp/img_ms.png");
			ms.mouseThrough=true;
			ms.width=panel.width;
			panel.mask=ms;
			btn_buzhou.on(Event.CLICK,this,changeStepState);
			
			_script.initData();
			//实验名称
			this.txt_name.text=Data.shiyanData[0].value;
			if(Data.shiyanData[0].value.length>17){
				txt_name.fontSize-=2;
			}
			if(txt_name.textWidth>336){
				this.titleBg.width=txt_name.textWidth+120;
			}
			//背景音乐
			btn_music.on(Event.CLICK,this,playPauseMusic);
			musicPlaying=true;
			function playPauseMusic():void
			{
				// TODO Auto Generated method stub
				if(musicPlaying){
					pauseMusic();
				}
				else{
					playMusic();
				}
			}
			
			dlg_tools=new ToolsBox();
			var bgScript:MouseOverShowMsg=btn_baogao.addComponent(MouseOverShowMsg);
			bgScript.direction="right";
			bgScript.value="实验报告";
			var jgScript:MouseOverShowMsg=btn_jieguo.addComponent(MouseOverShowMsg);
			jgScript.direction="right";
			jgScript.value="实验结果";
			var mdScript:MouseOverShowMsg=btn_mudi.addComponent(MouseOverShowMsg);
			mdScript.direction="left";
			mdScript.value="实验目的";
			var ylScript:MouseOverShowMsg=btn_yuanli.addComponent(MouseOverShowMsg);
			ylScript.direction="left";
			ylScript.value="实验原理";
			var ffScript:MouseOverShowMsg=btn_fangfa.addComponent(MouseOverShowMsg);
			ffScript.direction="left";
			ffScript.value="实验方法";
			var sxScript:MouseOverShowMsg=btn_zhuyishixiang.addComponent(MouseOverShowMsg);
			sxScript.direction="left";
			sxScript.value="注意事项";
			var qcScript:MouseOverShowMsg=btn_qicai.addComponent(MouseOverShowMsg);
			qcScript.direction="left";
			qcScript.value="实验器材";
			var sjScript:MouseOverShowMsg=btn_yaopin.addComponent(MouseOverShowMsg);
			sjScript.direction="left";
			sjScript.value="实验试剂";
			var kjScript:MouseOverShowMsg=btn_ppt.addComponent(MouseOverShowMsg);
			kjScript.direction="left";
			kjScript.value="实验课件";
			/**实验目的 */
			btn_mudi.on(Event.CLICK,JNEventDispatcher.getInstance(),JNEventDispatcher.getInstance().event,["mudi"]);
			/**实验原理*/
			btn_yuanli.on(Event.CLICK,JNEventDispatcher.getInstance(),JNEventDispatcher.getInstance().event,["yuanli"]);
			/**实验方法*/
			btn_fangfa.on(Event.CLICK,JNEventDispatcher.getInstance(),JNEventDispatcher.getInstance().event,["fangfa"]);
			/**注意事项*/
			btn_zhuyishixiang.on(Event.CLICK,JNEventDispatcher.getInstance(),JNEventDispatcher.getInstance().event,["zhuyishixiang"]);
			/**实验器材*/
			btn_qicai.on(Event.CLICK,this,shiyanQiCai);
			/**实验试剂*/
			btn_yaopin.on(Event.CLICK,this,shiyanQiCai);
			/**实验PPT课件*/
			btn_ppt.on(Event.CLICK,JNEventDispatcher.getInstance(),JNEventDispatcher.getInstance().event,["ppt"]);
			/**实验结果*/
			btn_jieguo.on(Event.CLICK,JNEventDispatcher.getInstance(),JNEventDispatcher.getInstance().event,["jieguo"]);
			/**实验报告*/
			btn_baogao.on(Event.CLICK,JNEventDispatcher.getInstance(),JNEventDispatcher.getInstance().event,["baogao"]);
			/**器材总集合*/
			qiCaiData=Data.shiyanQiCai.concat(Data.shiyanShiji);
			
			if(Data.isExam && Data.timeLimit){
				tip_djs.visible=true;
			}
		}
		
		public function playMusic():void
		{
			// TODO Auto Generated method stub
			SoundManager.playMusic(Data.bgMusic);
			btn_music.skin="comp/btn_sound_on.png";
			musicPlaying=true;
			rotate=true;
		}
		
		public function pauseMusic():void
		{
			// TODO Auto Generated method stub
			SoundManager.stopMusic();
			btn_music.skin="comp/btn_sound_off.png";
			musicPlaying=false;
			btn_music.rotation=0;
			rotate=false;
		}
		/**选择工具箱时触发的事件监听 */
		public function  chooseToolsHandler(choosedata:Object):void{
			if(Data.rightToolValue.length>0){
				if(choosedata.value==Data.rightToolValue){
					chooseRight();
				}
				else{
					showTips(chooseEroor);
				}
			}
		}
		public function closeToolsHandler():void
		{
			// TODO Auto Generated method stub
			hideTips();
			Tween.to(box_nextTool,{x:1920,alpha:0},400);
			Data.rightToolValue="";
			box_nextTool.offAll(Event.CLICK);
		}
		/**
		 * @param $steps
		 * 分析步骤信息
		 * 有子步骤的为 3*1、3*2、3*3
		 * 无子步骤的为 3*0 
		 * 
		 */		
		public function showStepByValue($steps:String):void{
			var temp:Array=$steps.split("*");
			var ind:int=parseInt(temp[0])-1;
			if(ind>=Data.stepList.length || ind<0){
				ind=0;
			}
			Data.currStep=ind;
			var sonstep:int=parseInt(temp[1])-1;
			if(Data.stepList[Data.currStep].value.length>0){//有二级步骤
				if(sonstep<0) {
					sonstep=0;
				}
				if(sonstep>Data.stepList[Data.currStep].value.length-1){
					sonstep=Data.stepList[Data.currStep].value.length-1;
				}
				Data.currSonStep=sonstep;
			}
			setStep();
		}
		/**
		 *提示操作步骤 
		 * @param str 提示信息
		 * @param bAutoHide  是否自动隐藏
		 * @param interval   如果自动隐藏，停留时间
		 * 
		 */		
		public function showTips(str:String,bAutoHide:Boolean=false,interval:int=5000):void{
			//有时配音后面需要跟一些动态数字，所以只读*前面的汉字，后面的数字不读出
			var temp:Array=str.split("*");
			if(Data.currSoundUrl && Data.currSoundUrl.length>0) SoundManager.stopSound(Data.currSoundUrl);
			Data.currSoundUrl="sound/peiyin/"+temp[0]+".mp3";
			SoundManager.playSound(Data.currSoundUrl,1);
			if(temp.length>1){
				lab_msg.text=temp[0]+temp[1];	
			}
			else lab_msg.text=temp[0];
			box_msg.alpha=1;
			box_msg.y=0;
			Tween.from(box_msg,{y:-100,alpha:0},600,Ease.circInOut);
			if(bAutoHide) Laya.timer.once(interval,this,hideTips);
		}
		/**
		 *隐藏操作提示信息 
		 * 
		 */		
		public function hideTips():void{
			Tween.to(box_msg,{y:-100,alpha:0},600,Ease.circInOut);
			trace("hide:"+lab_msg.text);
		}
		private var chooseEroor:String="";
		/**根据提示选择工具
		 * @ $value 工具的唯一键值
		 * @ $tips  选择工具时的提示
		 */
		public function getToolByValue($value:String,$tips:String):void{
			showTips($tips);
			Data.rightToolValue=$value;
			showNextTools();
			chooseEroor=$tips;
			box_nextTool.once(Event.CLICK,this,function():void{
				chooseRight();
			})
		}
		private function showNextTools():void{
			if(Data.rightToolValue.length>0){
				var i:int=0;
				for each (var data:Object in qiCaiData) 
				{
					// trace(data.value,ShiYanData.rightToolValue);
					if(data.value==Data.rightToolValue){
						Tween.to(box_nextTool,{x:1637,alpha:1},400);
						icon.skin=data.img;
						var f:GlowFilter=new GlowFilter("#000000",4,10,10);
						icon.filters=[f];
						icon.scaleY=icon.scaleX=60/icon.width;
						i++;
						break ;
					}
				}
				if(i!=1){
					trace("*****Error:找不到值为"+Data.rightToolValue+"的工具");
				}
				
			}
		}
		/**选择工具正确时的回馈 */
		private function chooseRight():void{
			JNEventDispatcher.getInstance().event(JNEventDispatcher.ClOSE_TOOLS);
			JNEventDispatcher.getInstance().event(JNEventDispatcher.CHOOSE_TOOLS_RIGHT);
		}
		/**
		 *实验器材 
		 * @param e
		 * 
		 */		
		private function shiyanQiCai(e:Event):void{
			if(e.currentTarget==btn_qicai)
			{
				dlg_tools.setData(0);
			}
			else{
				dlg_tools.setData(1);
			}
			dlg_tools.popup();
		}
		//背景音乐按钮自动旋转
		public function update():void{
			if(rotate){
				btn_music.rotation+=1.5;
			}
		}
		/**
		 *实验步骤按钮 
		 * 
		 */		
		private var isShowStep:Boolean=true;
		private function changeStepState():void
		{
			// TODO Auto Generated method stub
			isShowStep?hideStep():showStep();
			isShowStep=!isShowStep;
		}
		
		public  var currStepBtn:StepControl;
		private var sameStep:Boolean=false;
		/**
		 *初始化实验步骤  
		 * 
		 */
		private function setStep():void{
			for (var i:int = 0; i < Data.stepList.length; i++) 
			{
				var step:StepControl=new StepControl(i,Data.stepList[i].des);
				Box_tips.addChild(step);
				Box_tips.width=(step.width-20)*(i+1);
				stepControlList[i]=step;
				//考核模式不能点击跳转
				if(!Data.isExam){
					step.on(Event.CLICK,this,choose);
				}
			}
			panel.hScrollBarSkin="";
			panel.hScrollBar.elasticBackTime=300;
			panel.hScrollBar.elasticDistance=200;
			panel.hScrollBar.on(Event.CHANGE,this,laterRemove);
			Laya.timer.callLater(this,setBarPosition);
			Laya.timer.once(300,this,addSonStep);
			//选择步骤
			//如果有子步骤，点击一次打开子步骤列表，再点击一次关闭子步骤列表，默认子步骤存在5秒钟会自动关闭
			function choose(e:Event):void{
				var s:StepControl=e.currentTarget as StepControl;
				if(currStepBtn!=s){
					if(currStepBtn){
						currStepBtn.select=false;
					}
					sameStep=false;
					s.select=true;
					Data.currStep=s.value;
					Data.currSonStep=0;
					currSonStepBtn=null;
					//手动切换步骤事件
					JNEventDispatcher.getInstance().event(JNEventDispatcher.CLICK_ADD_PAGE);
					currStepBtn =s;
					
				}
				else{
					sameStep=true;
					if(isShowSonstep){
						laterRemove();
						return ;
					}
				}
				//增加二级子步骤
				addSonStep();
			}
		}
		/**
		 *根据索引显示步骤 一个步骤完成后自动跳转时使用
		 * @param currStep 步骤索引从0开始
		 * 
		 */		
		public function showStepByIndex($currStep:int):void
		{
			// TODO Auto Generated method stub
			Data.currStep=$currStep;
			setBarPosition();
			for each (var step:StepControl in stepControlList) 
			{
				if(step.value==$currStep){
					step.select=true;
					currStepBtn=step;
					Data.currSonStep=0;
					JNEventDispatcher.getInstance().event(JNEventDispatcher.NATURAL_ADD_PAGE);
					addSonStep();
				}
				else{
					step.select=false;
				}
			}
			
		}
		/**
		 *设置一级菜单的激活按钮的居中位置 
		 * 
		 */		
		private function setBarPosition():void
		{
			// TODO Auto Generated method stub
			var p:int=Data.currStep-3;
			if(p<0){
				p=0;
			}
			panel.hScrollBar.value=p/Data.stepList.length*Box_tips.width;
		}
		/**
		 *清理定时器并移除子步骤列表
		 * 
		 */		
		private function laterRemove():void
		{
			// TODO Auto Generated method stub
			Laya.timer.clear(this,laterRemove);
			removeSonSteps();
		}
		private var sonBg:Sprite=new Sprite();
		private var line:Sprite=new Sprite();
		private var currSonStepBtn:Button;
		private var isShowSonstep:Boolean=false;
		private var sonStepList:Array=[];
		
		private var ms:Image;
		/**
		 *背景音乐按钮是否旋转； 
		 */		
		private var rotate:Boolean=true;

		private var musicPlaying:Boolean;
		/**
		 *添加子步骤列表 
		 * 
		 */		
		private function addSonStep():void
		{
			laterRemove();
			if(Data.stepList[Data.currStep].value.length>0){
//				trace("增加二级按钮："+Data.stepList[Data.currStep].value);
				for (var i:int = 0; i < Data.stepList[Data.currStep].value.length; i++) 
				{
					var des:String=Data.stepList[Data.currStep].value[i];
					var btn:Button=new Button();
					btn.stateNum=2;
					btn.labelSize=17;
					btn.labelPadding="0,0,0,80";
					btn.labelColors="#ffffff,#ffffff,#ffffff";
					btn.clickHandler=null;
					btn.labelAlign="left";
					btn.sizeGrid="21,25,22,104";
					if(des.split("*")[1]){
						des=des.split("*")[0];
						btn.skin="comp/btn_二级标题3D.png";
					}
					else{
						btn.skin="comp/btn_二级标题.png";
					}
					btn.label=des;
					if(des.length>7){
						btn.width+=(des.length-7)*17;
					}
					btn.name=i.toString();
					var global:Point=currStepBtn.localToGlobal(new Point(0,0));
					stepBox.globalToLocal(global);
					btn.x=global.x+40;
					btn.y=img_line.y-100-40*i;
					line.graphics.clear();
					line.graphics.drawLine(btn.x+40,img_line.y,btn.x+40,btn.y+20,"#ffffff",2);
					stepBox.addChild(line);
					stepBox.addChild(sonBg);
					stepBox.setChildIndex(line,0);
					sonBg.addChild(btn);
					btn.name=i.toString();
					btn.alpha=0;
					btn.skewY=-120;
					line.alpha=0;
					Tween.to(line,{alpha:1},100,Ease.circInOut,null,300+i*100);
					Tween.to(btn,{skewY:0,alpha:1},300,Ease.circInOut,null,i*100);
					//考核模式不能点击跳转
					if(!Data.isExam){
						btn.on(Event.CLICK,this,choose);
					}
					sonStepList[i]=btn;
					if(i==Data.currSonStep){
						btn.selected=true;
						currSonStepBtn=btn;
					}
					else{
						btn.selected=false;
					}
					isShowSonstep=true;
				}
				//默认子步骤列表存在5秒钟，会自动关闭
				Laya.timer.once(5000,this,laterRemove);
			}
			//选择二级步骤
			function choose(e:Event):void{
				var tg:Button=e.currentTarget as Button;
				if(tg!=currSonStepBtn){
					if(currSonStepBtn) currSonStepBtn.selected=false;
					currSonStepBtn=tg;
					currSonStepBtn.selected=true;
					Data.currSonStep=Number(currSonStepBtn.name);
					//手动切换二级步骤事件
					JNEventDispatcher.getInstance().event(JNEventDispatcher.CLICK_SON_STEPS);
				}
				laterRemove();
			}
		}
		/**
		 *根据索引显示二级步骤步骤     一个二级步骤完成后自动跳转时使用
		 * @param $ind 步骤索引从0开始
		 * 
		 */		
		public function showSonStepByIndex($ind:int):void{
			for each (var btn:Button in sonStepList) 
			{
				if(btn.name==$ind+""){
					btn.selected=true;
					currSonStepBtn=btn;
					Data.currSonStep=$ind;
					JNEventDispatcher.getInstance().event(JNEventDispatcher.NATURAL_SON_STEPS);
				}
				else{
					btn.selected=false;
				}
			}
		}
		/**
		 *移除二级按钮 
		 * 
		 */		
		private function removeSonSteps():void{
			var i:int=0;
			for each (var btn:Button in sonStepList) 
			{
				btn.offAll(Event.CLICK);
				Tween.to(btn,{skewY:-120,alpha:1},300,Ease.circInOut,Handler.create(this,function():void{
					sonBg.destroyChildren();
					line.graphics.clear();
					line.alpha=0;
				}),i*100);
				i++;
			}
			isShowSonstep=false;
		}
		/**
		 *显示实验步骤 
		 * 
		 */		
		protected function showStep():void{
			trace("showStep");
			stepBox.visible=true;
			Tween.to(stepBox,{y:987,alpha:1},60,null,Handler.create(this,function():void{
				for each (var step:StepControl in stepControlList) 
				{
					step.show();
				}			
			}));
		}
		/**
		 *隐藏实验步骤 
		 * 
		 */		
		protected function hideStep():void{
			for each (var step:StepControl in stepControlList) 
			{
				step.hide();
			}
			Tween.to(stepBox,{y:1080,alpha:0},60,null,Handler.create(this,function():void{
				stepBox.visible=false;
			}),60);
		}
		/**
		 *更新时间 
		 * 
		 */		
		public function updateTime($time:String,$etime:String):void
		{
			// TODO Auto Generated method stub
			txt_time.text=$time;
			txt_djs.text=$etime;
		}
		
		public function getUserInfo(data:Object):void
		{
			// TODO Auto Generated method stub
			Data.CurrUser=new User(data.rows);
			var userNav:UserNav=new UserNav(Data.CurrUser.RealName,Data.CurrUser.HeadIcon);
			Laya.stage.addChild(userNav);
			userNav.mouseThrough=true;
			userNav.right=0;
			userNav.top=2;
		}
		
		
	}
}